package com.frame.work.lib;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.frame.work.entity.JointControlRelationDO;
import com.frame.work.entity.TblGateway;
import com.frame.work.mapper.JointControlRelationMapper;
import com.frame.work.mapper.TblDeviceMapper;
import com.frame.work.mapper.TblGatewayMapper;
import com.frame.work.mapper.TblGroupDeviceRelationMapper;
import com.frame.work.mqtt.Topic;
import com.frame.work.mqtt.data.handle.MqttDataHandle;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class LinkageControlRespondHandle {
    @Autowired
    private TblGroupDeviceRelationMapper groupDeviceRelationMapper;

    @Autowired
    private JointControlRelationMapper jointControlRelationMapper;

    @Autowired
    private TblGatewayMapper gatewayMapper;
    @Autowired
    private TblDeviceMapper deviceMapper;

    @Autowired
    private MqttDataHandle mqttDataHandle;

    @Autowired
    private StringRedisTemplate redisTemplate;

    public void processFrame(String gatewayMac, String data) {

        //接收联动返回的回应信息，如果网关执行成功，则进行数据库操作

        //设备mac
        String deviceMac = StringUtils.substring(data, 2, 18);
        //子索引
        String odIndex = StringUtils.substring(data, 22, 24);
        //状态
        String state = StringUtils.substring(data, 24, 26);
        System.out.println("设备mac："+deviceMac);
        System.out.println("odIndex："+odIndex);
        System.out.println("state："+state);
        System.out.println("data"+data);
        //00代表成功
        if ("00".equals(state)) {
            System.out.println("redis键值："+"zyzn_linkage_set_gatewayMac_" +gatewayMac + "_device_" + deviceMac+"_index_"+odIndex);
            String redisValue = redisTemplate.opsForValue().get("zyzn_linkage_set_gatewayMac_" +gatewayMac + "_device_" + deviceMac+"_index_"+odIndex);

            JSONObject jsonObject = new JSONObject();
            jsonObject = (JSONObject) JSONObject.parse(redisValue);
            Map<String, Object> returnMessage = new HashMap<>();
            returnMessage.put("gatewayMac", gatewayMac);
            returnMessage.put("startDeviceId", jsonObject.get("startDeviceId"));
            returnMessage.put("zigbeeMac", jsonObject.get("deviceMac"));
            returnMessage.put("odIndex", jsonObject.get("odIndex"));
            returnMessage.put("productType", jsonObject.get("productType"));
            returnMessage.put("controlType", jsonObject.get("controlType"));
            returnMessage.put("startSwitchState", jsonObject.get("startSwitchState"));
            returnMessage.put("endSwitchState", jsonObject.get("endSwitchState"));
            returnMessage.put("deviceType", jsonObject.get("deviceType"));
            returnMessage.put("type", "linkage");
            returnMessage.put("mode", jsonObject.get("mode"));
            returnMessage.put("state", state);
            System.out.println("接收到了redis的数据");
            /*try {
                Thread.sleep(150);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            mqttDataHandle.basePush(Topic.device.topics + gatewayMac + Topic.linkage, JSONObject.toJSONString(returnMessage));
            System.out.println("MQTT主题"+Topic.device.topics + gatewayMac + Topic.linkage);
            Boolean delete = redisTemplate.delete("zyzn_linkage_set_gatewayMac_" +gatewayMac + "_device_" + deviceMac+"_index_"+odIndex);
            if (delete){
                System.out.println("redis删除成功");
            }

        }
    }
}
